package info.magnolia.blossom.sample.cv.service;

import info.magnolia.blossom.sample.cv.model.CVSkills;
import info.magnolia.blossom.sample.cv.model.CurriculumVitae;
import info.magnolia.blossom.sample.cv.model.Experience;
import info.magnolia.context.MgnlContext;
import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
import org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl;
import org.apache.jackrabbit.ocm.mapper.Mapper;
import org.apache.jackrabbit.ocm.mapper.impl.annotation.AnnotationMapperImpl;
import org.apache.log4j.Logger;

import javax.jcr.Session;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: peterbalazs
 * Date: 2/25/13
 * Time: 2:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class OCMUtils {

    /**
     * Class logger.
     */
    private static final Logger logger = Logger.getLogger(OCMUtils.class);

    private static final List<Class> classList = Arrays.asList(new Class[] {CurriculumVitae.class, CVSkills.class, Experience.class});

    private OCMUtils() {
        // hide constructor
    }

    public static ObjectContentManager getObjectContentManager() {
        ObjectContentManager ocm = null;
        final Mapper mapper = new AnnotationMapperImpl(classList);
        try {
            final Session session = MgnlContext.getJCRSession("data");
            ocm = new ObjectContentManagerImpl(session, mapper);
        } catch (Exception e) {
            logger.error("Error creating Object Content Manager", e);
        }
        return ocm;
    }
}
